package com.tuanzhang.mall.seller.domain.service.impl;

import com.tuanzhang.mall.seller.domain.aggregate.Seller;
import com.tuanzhang.mall.seller.domain.service.SellerService;
import com.tuanzhang.mall.seller.infrastructure.repository.SellerRepository;
import com.tuanzhang.mall.seller.interfaces.facade.QixinBusinessFacade;
import com.tuanzhang.mall.seller.interfaces.facade.QixinBusinessResultInfo;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * 类功能描述
 *
 * @author wangyl
 * @date 2025/6/16
 */
@Service
public class SellerServiceImpl implements SellerService {

    private final SellerRepository sellerRepository;

    private final QixinBusinessFacade qixinBusinessFacade;

    public SellerServiceImpl(@Qualifier("mockSellerRepository") SellerRepository sellerRepository, QixinBusinessFacade qixinBusinessFacade) {
        this.sellerRepository = sellerRepository;
        this.qixinBusinessFacade = qixinBusinessFacade;
    }

    @Override
    public Seller addSeller(Seller seller) {
        //校验数据重复
        boolean flag = sellerRepository.existsPhoneNumber(seller.getSellerPhone());
        if (!flag) {
            throw new RuntimeException("手机号重复");
        }

        //校验公司信息校验
        boolean flag1 = sellerRepository.existsCompanyInfo(seller.getCompany());
        if (!flag1) {
            throw new RuntimeException("企业信息重复");
        }

        //调用启信宝查询企业信息
        QixinBusinessResultInfo qixinBusinessInfo = qixinBusinessFacade.getQixinBusinessInfo(seller.getCompany());
        seller.getCompany().checkCompanyLicenseId(qixinBusinessInfo);
        return seller;
    }
}
